define(["dojo/_base/declare", "dojox/grid/TreeGrid", "dojo/data/ItemFileReadStore", "dojo/data/ItemFileWriteStore"], function(declare){
    declare("my.grid.GradesGrid", [dijit.form.Form], {

    	summaryFormatter : function(value, rowIdx, cell, sing, plur){
			var str;
			if(rowIdx >= 0){
				return value;
			}
			if(rowIdx == -1){
				str = "Total (${numItems} ${displayName}):";
			}else{
				str = "${numItems} ${displayName}";
			}
			return dojo.string.substitute(str, {numItems: value, 
						displayName: (value == 1) ? sing : plur});			
		},
		
		dotFormatter : function(value, rowIdx){
			if(rowIdx >= 0){
				return value;
			}
			return "";
		},
		
		classesnSummary : function(value, rowIdx){
			return "...";
		},
		
		getStore : function(){
			return new dojo.data.ItemFileWriteStore(
		    	    {
		    	    	url:'getDegreeReportAcion.action?classesFilter.id='+
		    	    	dijit.byId('classesFilter').get('value')+
		    	    	'&studentFilter.id=' + dijit.byId('studentFilter').get('value'),
		 			    requestMethod : "post"
		    	    });
		},
		
		resetGrid : function(){
			dijit.byId("grid").setStore(new dojo.data.ItemFileWriteStore(
		    	    {
		    	    	url:'getDegreeReportAcion.action',
		 			    requestMethod : "post"
		    	    }));
		},
		
		connectDropDowns : function(){
			var classesDropDown = dijit.byId("classesFilter");
			dojo.connect(classesDropDown, "onChange", this, function(){
				var grid = dijit.byId("grid");
				grid.setStore(this.getStore());
			});
			
			var studentsDropDown = dijit.byId("studentFilter");
			dojo.connect(studentsDropDown, "onChange", this, function(){
				var grid = dijit.byId("grid");
				grid.setStore(this.getStore());
			});
		},
		
		startup : function(){
			
			var grid = dijit.byId("grid");
			var structure =  [{ cells: [[
			                             { field: "label", name: "Classes", width: "22em"},
			                             { field: "students", 
			                            	 children:[
			                            	           { field: "label", name: "Student", width: "13em", formatter: this.dotFormatter},
			                            	           { field: "grades", 
			                            	        	   children:[
			                            	        	              	{field: "type", name: "Type"},
			                            	        	              	{field: "grade", name: "Grade"},
			                            	        	              	{field: "desc", name: "Description", width: "100%", formatter: this.dotFormatter}
			                            	        	              ],
			                            	        	              itemAggregates: ["fun", "avg"]
			                            	           }
			                            	           ]
			                             }
			                             ]] }];
			this.inherited(arguments);
    	    grid.set("structure", structure);
    	    grid.startup();
    	    
    	    var reportStore = this.getStore();
    	    var mockStore = new dojo.data.ItemFileWriteStore({url:'gamedata.json'});
    	    grid.setStore(reportStore);
    	    this.connectDropDowns();
		}
    	
    });
});